package visualcbir.node;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.util.Observable;
import visualcbir.util.VSUtils;
import visualcbir.IBackStructureView;
import visualcbir.View;

/**
 *
 * @author sheila
 */
public class ViewNode extends View implements IViewNode {
    private Image oImage;    
    private int nodeBorder=3;
    private Color nodeColor;//sh//
    private boolean isBeingCreated=true;

    public ViewNode(ModelNode poNodeModel){
        super(poNodeModel);
    }

    @Override
    public void drawNode(Graphics g){
        this.drawNode(g,this.nodeColor);
    }

    /**
     * //obs//Aqui pode se ter um so array com todas as imagens carregadas e escalar elas conforme forem displayadas.
     * @param g
     * @param nodeColor
     */
    @Override
    public void drawNode(Graphics g, Color nodeColor){
        try{
        if(isBeingCreated)  // Clave da demora dos métodos de clustering
            oImage = VSUtils.setImageScaleFactor( VSUtils.readImage( ((IModelNode)oModel).getStrPath() ), super.getScaleFactor() );
            isBeingCreated=false;
        }catch(Exception e){
            System.err.println("Erro drawNode: "+e.getLocalizedMessage()); return;
        }

        ImageObserver imgObs=new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                //System.out.println("NodeVIEW------------>Image unloaded");
                //System.out.println("infoflags:"+infoflags+" - x:"+x+" - y:"+y +" - width:"+width+" - height:"+height);
                return true;
            }
        };
        int x = this.getCenterPoint().x;
        int y = this.getCenterPoint().y;

        int w = oImage.getWidth(null);
        int h = oImage.getHeight(null);

        //**** Drawing the image ***//
        w+=6; h+=6;
        g.setColor(this.nodeColor);
        g.fillRect(x-w/2, y-h/2, w, h);
        g.setColor(Color.red);
        g.drawRect(x-w/2, y-h/2, w-1, h-1);
        w-=6;h-=6;
        g.drawImage(oImage, x-w/2, y-h/2, imgObs);
            
    }

    public void setColor(Color oNodeColor){    this.nodeColor=oNodeColor;   }


    @Override
    public double getNodeWidth(){

        double w=oImage.getWidth(null);
        if(w==-1){  
            w=80; System.out.println("ALGUMA VEZ AQUI");
        }
        return w+this.nodeBorder*2;
    }

    @Override
    public double getNodeHeight(){        
        double h=oImage.getHeight(null);
        if(h==-1)   h=100;
        return h+this.nodeBorder*2;
    }

    @Override
    public void makeViewFactory() {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void drawViewFactory(Object pArg) {
        this.drawNode((Graphics)pArg);
    }

    @Override
    public void update(Observable o, Object arg) {
        //shi//
        super.setViewDimension( (int)Math.round(getNodeWidth()), (int)Math.round(this.getNodeHeight()));
    }

    @Override
    public IBackStructureView makeBackStructureView() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public IViewNode getCenterNode() {
        return this;
    }

    @Override
    public Dimension computeViewDimension() {
        //super.setViewDimension( (int)Math.round(getNodeWidth()), (int)Math.round(this.getNodeHeight()));
        //shi//return this.getViewDimension();
        throw new UnsupportedOperationException("Not supported yet.");
        
    }




    
//    public void setCenterPoint(Point poCenterPoint) {
//        this.centerPoint = poCenterPoint;
//        this.ix = poCenterPoint.x;
//        this.iy = poCenterPoint.y;
//    }
    //public int getIx() {     return ix;    }
    //public void setIx(int ix) {        this.ix = ix;    }
    //public int getIy() {        return iy;    }
    //public void setIy(int iy) {        this.iy = iy;    }
  /*public double getImageWidth(){
        Image oImage=VSUtils.readImage(oINodeModel.getStrUrl());
        double w=oImage.getWidth(null);
        if(w==-1){   w=80; System.out.println("ALGUMA VEZ AQUI");}
        return w;
    }

    public double getImageHeight(){
        Image oImage=VSUtils.readImage(oINodeModel.getStrUrl());
        double h=oImage.getHeight(null);
        if(h==-1)   h=100;
        return h;
    }

    public double getNodeWidth(){
        Image oImage=VSUtils.readImage(oINodeModel.getStrUrl());
        double w=oImage.getWidth(null);
        if(w==-1){   w=80;System.out.println("ALGUMA VEZ AQUI");}
        return w+this.nodeBorder;
    }
    public double getNodeHeight(){
        Image oImage=VSUtils.readImage(oINodeModel.getStrUrl());
        double h=oImage.getHeight(null);
        if(h==-1)   h=100;
        return h+this.nodeBorder;
    }*/

}
